import * as types from './mutations-types';
import {
  playMode
} from 'common/js/config';
import {
  shuflle
} from 'common/js/util';
import {
  saveSearchArr,
  delateOne,
  allDelateFn,
  savePlayArr,
  saveFavoriteArr,
  delateOneFavorite
} from 'common/js/cache';

function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.mid == song.mid
  })
}

export const selectPlay = function ({
  commit,
  state
}, {
  list,
  index
}) {
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shuflle(list)
    commit(types.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)
  }
  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}


export const randomPlay = function ({
  commit
}, {
  list
}) {
  commit(types.SET_PLAY_MODE, playMode.random)
  commit(types.SET_SEQUENCE_LIST, list)
  let randomList = shuflle(list)
  commit(types.SET_PLAYLIST, randomList)
  commit(types.SET_CURRENT_INDEX, 0)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

// 歌曲
export const insertSong = function ({
  commit,
  state
}, song) {
  // .slice() 创建副本
  let playList = state.playList.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // playList 操作
  // 记入当前歌曲的信息
  const saveSong = playList[currentIndex]
  // 记入playList 里面歌曲的位置
  const playListIndex = findIndex(playList, song)
  // 给当前播放索引 +1
  currentIndex++
  // 给 playLIst 添加 歌曲
  playList.splice(currentIndex, 0, song)
  // 判断是否存在这首歌
  if (playListIndex > -1) {
    // 当前插入的位置 与 本来就有这首歌的位置进行对比
    if (currentIndex > playListIndex) {
      // 如果插入的位置 在 本来歌曲之后
      // 删掉原来歌曲 索引减1
      playList.splice(playListIndex, 1)
      currentIndex--
    } else {
      // 如果插入的歌曲在前面 就删除后面的歌曲
      playList.splice(playListIndex + 1, 1)
    }
  }

  // sequenceList 的操作
  // 找到当前播放歌曲的索引
  const sequenceListIndex = findIndex(sequenceList, saveSong) + 1
  // 判断是否存在添加的歌曲
  const sequenceListSong = findIndex(sequenceList, song)
  // 添加歌曲
  sequenceList.splice(sequenceListIndex, 0, song)
  // 判断是否存在歌曲
  if (sequenceListSong > -1) {
    // 当前插入歌曲位置进行对比
    if (sequenceListIndex > sequenceListSong) {
      sequenceList.splice(sequenceListSong, 1)
    } else {
      // 因为插入的歌曲 所以的加一
      sequenceList.splice(sequenceListSong + 1, 1)
    }
  }

  // 最后提交数据
  commit(types.SET_PLAYLIST, playList)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}

// 搜索历史
//保存历史
export const saveSearchHistory = function ({
  commit
}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearchArr(query))
}
// 删除一条数据
export const delateSearchHistory = function ({
  commit
}, query) {
  commit(types.SET_SEARCH_HISTORY, delateOne(query))
}

// 删除全部数据
export const allSearchHistory = function ({
  commit
}) {
  commit(types.SET_SEARCH_HISTORY, allDelateFn())
}


// 删除播放列表一条数据
export function delateOneSong({
  commit,
  state
}, song) {
  // 获取数据
  let playList = state.playList.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // 删除
  const pIndex = findIndex(playList, song)
  playList.splice(pIndex, 1)
  const sIndex = findIndex(sequenceList, song)
  sequenceList.splice(sIndex, 1)
  // 如果删除的数据在播放歌曲之前
  if (currentIndex > sIndex || currentIndex == playList.length) {
    currentIndex--
  }
  // 如果全部删完了
  let indexLength = playList.length > 0
  // 提交
  commit(types.SET_PLAYLIST, playList)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_PLAYING_STATE, indexLength)
}

export function clearSongList({
  commit
}) {
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYING_STATE, false)
}


// 播放记录
export const savePlayHistory = function ({
  commit
}, query) {
  commit(types.SET_PLAY_HISTORY, savePlayArr(query))
}


//收藏
export const saveFavoriteList = function ({
  commit
}, query) {
  commit(types.SET_FAVORITE_LIST, saveFavoriteArr(query))
}

// 取消收藏
export const delateFavoriteList = function ({
  commit
}, query) {
  commit(types.SET_FAVORITE_LIST, delateOneFavorite(query))
}
